package com.gitee.wsl.transform.encode.pinyin.interval


/**
 * Responsible for tracking the start and end bounds, which are reused by
 * both [Emit] and [PayloadEmit].
 */
open class Interval
/**
 * Constructs an interval with a start and end position.
 *
 * @param start The interval's starting text position.
 * @param end   The interval's ending text position.
 */(
    /**
     * Returns the starting offset into the text for this interval.
     *
     * @return A number between 0 (start of text) and the text length.
     */
    override val start: Int,
    /**
     * Returns the ending offset into the text for this interval.
     *
     * @return A number between getStart() + 1 and the text length.
     */
    override val end: Int
) : Intervalable {
    /**
     * Returns the length of the interval.
     *
     * @return The end position less the start position, plus one.
     */
    override val size: Int
        get() = end - start + 1

    /**
     * Answers whether the given interval overlaps this interval
     * instance.
     *
     * @param other the other interval to check for overlap
     * @return true The intervals overlap.
     */
    fun overlapsWith(other: Interval): Boolean {
        return this.start <= other.end &&
                this.end >= other.start
    }

    fun overlapsWith(point: Int): Boolean {
        return this.start <= point && point <= this.end
    }


    override fun equals(other: Any?): Boolean {
        if (other !is Intervalable) {
            return false
        }
        val other: Intervalable = other as Intervalable
        return this.start == other.start &&
                this.end == other.end
    }


    override fun hashCode(): Int {
        return this.start % 100 + this.end % 100
    }


    fun compareTo(o: Interval?): Int {
        if (o !is Intervalable) {
            return -1
        }
        val other: Intervalable = o as Intervalable
        val comparison: Int = this.start - other.start
        return if (comparison != 0) comparison else this.end - other.end
    }

    /**
     * Returns the starting offset and ending offset separated
     * by a full colon (:).
     *
     * @return A non-null String, never empty.
     */
    override fun toString(): String {
        return this.start.toString() + ":" + this.end
    }
}
